#include "QtUI/QtConnectionConfigWidget.qth"
#include "QtUI/QtConnectionConfigWidget.moh"
#include "Storage/ConnectionConfig.hpp"
#include <QtGui/QGridLayout>
#include <QtGui/QLineEdit>
#include <QtGui/QLabel>
#include <QtGui/QVBoxLayout>
#include <cassert>

namespace QtUI
{

QtConnectionConfigWidget::QtConnectionConfigWidget( QWidget* pParent /*= NULL*/ )
	: QWidget( pParent)
	, nameWasEmpty_( true)
{
	QGridLayout* pLayout = new QGridLayout();
	this->setLayout( pLayout);
	pLayout->setMargin(0);

	pNameEditor_ = new QLineEdit();
	QLabel* pConnNameLabel = new QLabel( pNameEditor_);
	pConnNameLabel->setText( tr( "Name", "Current connection name label"));
	pLayout->addWidget( pConnNameLabel, 0, 0);
	pLayout->addWidget( pNameEditor_, 0, 1);

	QVBoxLayout* pSpacerLayout = new QVBoxLayout();
	pSpacerLayout->addStretch();
	pLayout->addLayout( pSpacerLayout, 1, 0, 1, 2);

	connect( pNameEditor_, SIGNAL( textChanged( const QString&)), this, SLOT( handleNameTextChanged( const QString& )));
}

// ---------------------------------------------------------------------------------------------------------------------

void QtConnectionConfigWidget::handleNameTextChanged( const QString& newName)
{
	bool newNameIsEmpty = newName.isEmpty();
	if( nameWasEmpty_ != newNameIsEmpty)
	{
		nameWasEmpty_ = newNameIsEmpty;
		Q_EMIT nameDefined( newNameIsEmpty);
	}
}

// ---------------------------------------------------------------------------------------------------------------------

QString QtConnectionConfigWidget::connectionName() const
{
	return pNameEditor_->text();
}

// ---------------------------------------------------------------------------------------------------------------------

void QtConnectionConfigWidget::setEditedConfig( Storage::ConnectionConfig* pEditedConfig )
{
	assert( NULL == pEditedConnConfig_ );
	assert( NULL != pEditedConfig);
	pEditedConnConfig_ = pEditedConfig;
	pNameEditor_->setText( QString::fromStdString( pEditedConfig->name()));
	this->setDirty( false);
}

// ---------------------------------------------------------------------------------------------------------------------


} // namespace QtUI
